﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
namespace Athena.RouteAddress.Implementations
{
    using Serialization;
    public class DefaultRouteDescriptorChanger : IRouteDescriptorChanger
    {
        private readonly ISerializer<string> _serializer;
        private readonly ILogger<DefaultRouteDescriptorChanger> _logger;
        public DefaultRouteDescriptorChanger(ISerializer<string> serializer, ILogger<DefaultRouteDescriptorChanger> logger)
        {
            _serializer = serializer;
            _logger = logger;
        }

        public Route CreateRoute(RouteDescriptor routeDescriptor)
        {
            var type = Type.GetType(routeDescriptor.Type);
            return (Route)_serializer.Deserialize(routeDescriptor.Value, type);
        }

        public RouteDescriptor CreateRouteDescriptor(Route baseRoute)
        {
            return new RouteDescriptor()
            {
                Type = baseRoute.GetType().FullName,
                Value = _serializer.Serialize(baseRoute),
            };
        }

        public ServiceRoute CreateServiceRoute(ServiceRouteDescriptor serviceRouteDescriptor)
        {
            var baseRouteList = new List<Route>();
            foreach (var item in serviceRouteDescriptor.RouteDescriptors)
            {
                baseRouteList.Add(CreateRoute(item));
            }
            return new ServiceRoute()
            {
                CorrelationId = serviceRouteDescriptor.CorrelationId,
                Routes = baseRouteList
            };
        }
    }
}
